﻿/**
 * Browser相关操作
 * @namespace Browser
 * @name core.Browser
 * @static
 */
core.Browser = {
  /**
   * 判断浏览器是否IE内核
   * @name core.Browser.IE
   * @property IE
   * @grammar core.Browser.IE
   *
   * @return {Boolean} 是否IE内核
   */
  IE: !!(window.attachEvent && !window.opera),
  /**
   * 判断浏览器是否IE6内核
   * @name core.Browser.IE6
   * @property IE6
   * @grammar core.Browser.IE6
   *
   * @return {Boolean} 是否IE6内核
   */
  IE6: (/msie\s*(\d+)\.\d+/g.exec(navigator.userAgent.toLowerCase()) || [0, "0"])[1] == "6",
  /**
   * 判断浏览器是否IE7内核
   * @name core.Browser.IE7
   * @property IE7
   * @grammar core.Browser.IE7
   *
   * @return {Boolean} 是否IE7内核
   */
  IE7: navigator.userAgent.indexOf('MSIE 7.0') > -1,
  /**
   * 判断浏览器是否IE8内核
   * @name core.Browser.IE8
   * @property IE8
   * @grammar core.Browser.IE8
   *
   * @return {Boolean} 是否IE8内核
   */
  IE8: navigator.userAgent.indexOf('MSIE 8.0') > -1,
  /**
   * 判断浏览器是否垃圾搜狗浏览器
   * @name core.Browser.Sogou
   * @property Sogou
   * @grammar core.Browser.Sogou
   *
   * @return {Boolean} 是否Sogou垃圾浏览器
   */
  Sogou: navigator.userAgent.indexOf('SE 2.X') > -1,
  /**
   * 判断浏览器是否Sogou IE7内核
   * @name core.Browser.Sogou
   * @property Opera
   * @grammar core.Browser.Sogou
   *
   * @return {Boolean} 是否Sogou IE7内核
   */
  Opera: !!window.opera,
  /**
   * 判断浏览器是否WebKit内核
   * @name core.Browser.WebKit
   * @property WebKit
   * @grammar core.Browser.WebKit
   *
   * @return {Boolean} 是否WebKit内核
   */
  WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
  /**
   * 判断浏览器是否Gecko内核
   * @name core.Browser.Gecko
   * @property Gecko
   * @grammar core.Browser.Gecko
   *
   * @return {Boolean} 是否Gecko内核
   */
  Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
  /**
   * 判断浏览器是否Safari浏览器
   * @name core.Browser.Safari
   * @property Safari
   * @grammar core.Browser.Safari
   *
   * @return {Boolean} 是否Safari浏览器
   */
  Safari: navigator.userAgent.indexOf('Safari') != -1,
  /**
   * 判断浏览器是否ipad/ipod/iphone端浏览器
   * @name core.Browser.Mobile
   * @property Mobile
   * @grammar core.Browser.Mobile
   *
   * @return {Boolean} 是否ipad/ipod/iphone端浏览器
   */
  Mobile: 'createTouch' in document && !('onmousemove' in document.documentElement) || /(iPhone|iPad|iPod)/i.test(navigator.userAgent),
  /**
   * 获取浏览器名称
   * @name core.Browser.getName
   * @property getName
   * @grammar core.Browser.getName
   *
   * @return {String} 浏览器名称
   */
  getName: function () {
    var _a = "",
      _n = navigator.userAgent.toLowerCase(),
      _c = function (browser) {
        return _n.indexOf(browser) > -1;
      },
      _b = (
        _c('opera') === true ? 'opera' :
          (_c('msie') && _c('360se')) === true ? '360se' :
            ((_c('msie') && _c('tencenttraveler')) && _c('metasr')) === true ? "sogobrowser" :
              (_c('msie') && _c('qqbrowser')) === true ? "QQbrowser" :
                (_c('msie') && _c('tencenttraveler')) === true ? "TTbrowser" :
                  _c('msie') === true ? 'msie' :
                    _c('se 2.x') === true ? "sogou" :
                      (_c('safari') && !_c('chrome')) === true ? 'safari' :
                        _c('maxthon') === true ? "maxthon" :
                          ((_c('chrome') && _c('safari')) && _c('qihu 360ee')) === true ? "360ee" :
                            (_c('chrome') && _c('taobrowser')) === true ? "taobrowser" :
                              _c('chrome') === true ? "chrome" :
                                ((_c('gecko') && !_c('webkit')) && _c('seamonkey')) === true ? "SeaMonkey" :
                                  ((_c('gecko') && !_c('webkit')) && !_c('netscape')) === true ? 'firefox' :
                                    ((_c('gecko') && !_c('webkit')) && _c('netscape')) === true ? 'netscape' :
                                      "other").toLowerCase();
    switch (_b) {
      case "360se":
      case "qihu 360ee":
      case "sogou":
        _a = _b;
        break;
      case "opera":
      case "safari":
      case "firefox":
      case "qqbrowser":
      case "seamonkey":
      case "taobrowser":
        _a = _b + _n.substring(_n.lastIndexOf("/"));
        break;
      case "netscape":
      case "chrome":
        _a = _b + _n.substring(_n.lastIndexOf("/"), _n.lastIndexOf(" "));
        break;
      case "maxthon":
        _a = _b + _n.substring(_n.lastIndexOf("/"), _n.lastIndexOf("chrome"));
        break;
      case "ttbrowser":
        _a = _b + _n.substring(_n.lastIndexOf("/"), _n.lastIndexOf(")"));
        break;
      case "msie":
        _a = _n.substring(_n.lastIndexOf(_b)).substring(0, _n.substring(_n.lastIndexOf(_b)).indexOf(";"));
        break;
      default:
        _a = _b;
    }
    return _a;
  },
  /**
   * 加入收藏夹
   * @name core.Browser.addFavorate
   * @function
   * @grammar core.Browser.addFavorate(name, url)
   * @param {String} name 收藏名称
   * @param {String} url 收藏URL地址
   */
  addFavorate: function (name, url) {
    if (this.IE) {
      window.external.addFavorite(url, name);
    } else if (window.sidebar) {
      window.sidebar.addPanel(name, url, "");
    }
  },
  /**
   * 设置首页
   * @name core.Browser.setHomepage
   * @function
   * @grammar core.Browser.setHomepage(element, url)
   * @param {HTMLElement} element 页面中的行为触发源
   * @param {String} url 要设为首页的URL地址
   */
  setHomepage: function (element, url) {
    try {
      element.style.behavior = "url(#default#homepage)";
      element.setHomePage(url);
    } catch (e) {
      if (window.netscape) {
        try {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
          console.log('warning:[about:config][signed.applets.codebase_principal_support = true]');
        }
      }
    }
  },
  /**
   * 复制到剪贴板
   * @name core.Browser.copy
   * @function
   * @grammar core.Browser.copy()
   */
  copy: function (o) {
    function onfail() {
      if (core.Object.isElement(o)) {
        o.select();
      }
    }

    var str;
    if (core.Object.isElement(o)) {
      str = o.value;
    } else {
      str = o;
    }
    if (window.clipboardData && clipboardData.setData) {
      if (clipboardData.setData('text', str)) return true;
    } else {
      console.log('Your browser does not support script replication. Please try to copy manually');
      return false;
    }
    console.log('Your browser settings do not allow scripts to access the clipboard');
    return false;
  }
};
